package com.xvyy.tingshu.album.service.impl;


import com.xvyy.tingshu.album.mapper.AlbumStatMapper;
import com.xvyy.tingshu.album.mapper.TrackStatMapper;
import com.xvyy.tingshu.album.service.MqOpsService;
import com.xvyy.tingshu.album.service.TrackInfoService;
import com.xvyy.tingshu.common.constant.SystemConstant;
import com.xvyy.tingshu.common.execption.TsException;
import com.xvyy.tingshu.common.result.Result;
import com.xvyy.tingshu.model.order.OrderInfo;
import com.xvyy.tingshu.order.client.OrderInfoFeignClient;
import com.xvyy.tingshu.vo.album.TrackStatMqVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * ClassName: MqOpsServiceImpl
 * Package: com.xvyy.tingshu.album.service.impl
 *
 * @Description:
 * @Author: xvyy
 * @Create: 2025/2/5 - 20:19
 * @Version: v1.0
 */
@Service
@Slf4j
public class MqOpsServiceImpl implements MqOpsService {

    @Autowired
    private AlbumStatMapper albumStatMapper;
    @Autowired
    private TrackStatMapper trackStatMapper;
    @Autowired
    private OrderInfoFeignClient orderInfoFeignClient;
    @Autowired
    private TrackInfoService trackInfoService;

    @Override
    @Transactional(rollbackFor = Exception.class)// 开启事务管理 异常回滚
    public void listenAlbumAndTrackStatNumUpdate(TrackStatMqVo trackStatMqVo) {
        Long albumId = trackStatMqVo.getAlbumId();
        String statType = trackStatMqVo.getStatType();
        Integer count = trackStatMqVo.getCount();
        Long trackId = trackStatMqVo.getTrackId();
        try {
            // 播放量更新
            if (albumId != null) {
                // 1. 更新专辑播放量
                int updatedFirst = albumStatMapper.updateAlbumPlayCount(albumId, statType, count);
                log.info("更新专辑播放量{}", updatedFirst > 1 ? "成功" : "失败");
                // 2. 更新专辑曲目播放量
                int updatedSecond = trackStatMapper.updateTrackPlayCount(trackId, SystemConstant.TRACK_STAT_PLAY, count);
                log.info("更新专辑曲目播放量{}", updatedSecond > 1 ? "成功" : "失败");
            } else {
                // 收藏量更新
                int updatedThird = trackStatMapper.updateTrackPlayCount(albumId, statType, count);
                log.info("更新专辑收藏量{}", updatedThird > 1 ? "成功" : "失败");
            }
        } catch (Exception e) {
            throw new TsException(201, "数据库操作失败" + e.getMessage());
        }
    }

    @Override
    public void albumAndBuyNumUpdate(String orderNo) {
        // 1. 远程调用order微服务，获取订单信息
        Result<OrderInfo> orderInfoResult = orderInfoFeignClient.getOrderInfo(orderNo);
        OrderInfo orderInfoData = orderInfoResult.getData();
        Assert.notNull(orderInfoData, "远程调用order微服务，获取订单信息失败");
        String itemType = orderInfoData.getItemType();
        Long albumId = null;
        int count = 0;
        // 2. 判断订单类型，获取专辑id
        switch (itemType) {
            case SystemConstant.ALBUM_PAY_TYPE_FREE -> {// 专辑
                albumId = orderInfoData.getOrderDetailList().get(0).getItemId();
                // 更新专辑购买量
                count = albumStatMapper.updateAlbumPlayCount(albumId, SystemConstant.ALBUM_STAT_BROWSE, orderInfoData.getOrderDetailList().size());
            }
            case SystemConstant.ALBUM_PAY_TYPE_VIPFREE -> {// 声音
                Long trackId = orderInfoData.getOrderDetailList().get(0).getItemId();
                // 更新专辑购买量
                albumId = trackInfoService.getById(trackId).getAlbumId();
                count = albumStatMapper.updateAlbumPlayCount(albumId, SystemConstant.ALBUM_STAT_BROWSE, orderInfoData.getOrderDetailList().size());
            }
        }//"1003" vip不用处理
        log.info("更新专辑购买量{}", count > 1 ? "成功" : "失败");
    }

}
